#!/usr/bin/perl

use Cwd;

$baseurl = getcwd;

if ($ARGV[0] eq 'CREATING') {
    $mode = 'CREATING';
} else {
    $mode = 'UPDATING';
}

if ($ARGV[1] =~ /\//) {
    ($baseurl = $ARGV[1]) =~ s/[\/]*$//;
}

$error_outs = $baseurl . '/ONEW_errors.txt';
$print_outs = $baseurl . '/ONEW_prints.txt';

binmode(STDOUT, ':utf8');

open (STDERR, ">$error_outs");
open (STDOUT, ">$print_outs");


print "baseurl: " . $baseurl . "\n";

# local system variables
$connect = $baseurl . '/connect.pl';
require $connect;

use DBI;

use IO::File;

#use Data::Dumper;

use utf8;

use Unicode::Normalize;

use MARC::Charset 'marc8_to_utf8';

use MARC::Batch;

#use strict;
#  use Carp ();
#    local $SIG{__WARN__} = \&Carp::cluck;

&hashes_and_arrays; #SUBROUTINE AT END OF SCRIPT

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$start_time = "$HOUR:$MIN:$SEC";


@current_time = localtime;

# connection for the select
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass, { mysql_enable_utf8 => 1 }) or die "couldn't connect to database: " . DBI->errstr;

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$start_external_update_time = "$HOUR:$MIN:$SEC";

$last_update_time = ($current_time[5] + 1900) . "-" . ($current_time[4] + 1) . "-$current_time[3] $current_time[2]:$current_time[1]:$current_time[0]";


foreach $folder (consortium_bibs_textfiles, consortium_bibs_deleted) {

    $where = $baseurl . '/' . $folder;

while (defined($next_file = <$where/*.key>)) {

    open(IN, "< $next_file") || die "cannot open $next_file for reading: $!";

            while(<IN>) {
                $mysql_keys = $_;
                chomp($mysql_keys);
            }
    close(IN) || "cannot close $next_file: $!";

unlink($next_file);

}

while (defined($next_file = <$where/*.txt>)) {

#print "LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'bibs_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")\n";

   $sth1 = $dbh->do("LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'bibs_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")") || print "died again sth1 consortium\n";

unlink($next_file);

}

}


foreach $folder (consortium_holdings_textfiles, consortium_holdings_deleted) {

    $where = $baseurl . '/' . $folder;

while (defined($next_file = <$where/*.key>)) {

    open(IN, "< $next_file") || die "cannot open $next_file for reading: $!";

            while(<IN>) {
                $mysql_keys = $_;
                chomp($mysql_keys);
            }
    close(IN) || "cannot close $next_file: $!";

unlink($next_file);

}

while (defined($next_file = <$where/*.txt>)) {

   $sth2 = $dbh->do("LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'holdings_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")") || print "died again sth2 consortium\n";

unlink($next_file);

}

}


foreach $folder (consortium_items_textfiles, consortium_items_deleted) {

    $where = $baseurl . '/' . $folder;

while (defined($next_file = <$where/*.key>)) {

    open(IN, "< $next_file") || die "cannot open $next_file for reading: $!";

            while(<IN>) {
                $mysql_keys = $_;
                chomp($mysql_keys);
            }
    close(IN) || "cannot close $next_file: $!";

unlink($next_file);

}

while (defined($next_file = <$where/*.txt>)) {

   $sth3 = $dbh->do("LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'items_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")") || print "died again sth3 consortium\n";

unlink($next_file);

}

}

foreach $folder (consortium_checkins_textfiles, consortium_checkins_deleted) {

    $where = $baseurl . '/' . $folder;

while (defined($next_file = <$where/*.key>)) {

    open(IN, "< $next_file") || die "cannot open $next_file for reading: $!";

            while(<IN>) {
                $mysql_keys = $_;
                chomp($mysql_keys);
            }
    close(IN) || "cannot close $next_file: $!";

unlink($next_file);

}

while (defined($next_file = <$where/*.txt>)) {

   $sth4 = $dbh->do("LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'checkins_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")") || print "died again sth4 consortium\n";

unlink($next_file);

}

}

foreach $folder (consortium_orders_textfiles, consortium_orders_deleted) {

    $where = $baseurl . '/' . $folder;

while (defined($next_file = <$where/*.key>)) {

    open(IN, "< $next_file") || die "cannot open $next_file for reading: $!";

            while(<IN>) {
                $mysql_keys = $_;
                chomp($mysql_keys);
            }
    close(IN) || "cannot close $next_file: $!";

unlink($next_file);

}

while (defined($next_file = <$where/*.txt>)) {

   $sth4 = $dbh->do("LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'orders_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")") || print "died again sth4 consortium\n";

unlink($next_file);

}

}

foreach $folder (consortium_authorities_textfiles, consortium_authorities_deleted) {

    $where = $baseurl . '/' . $folder;

while (defined($next_file = <$where/*.key>)) {

    open(IN, "< $next_file") || die "cannot open $next_file for reading: $!";

            while(<IN>) {
                $mysql_keys = $_;
                chomp($mysql_keys);
            }
    close(IN) || "cannot close $next_file: $!";

unlink($next_file);

}

while (defined($next_file = <$where/*.txt>)) {

#print "LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'authorities_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")\n";

   $sth1 = $dbh->do("LOAD DATA INFILE '" . $next_file . "' REPLACE INTO TABLE $db_consortium_tables_hash{'authorities_record_table'} FIELDS TERMINATED BY '\t' (" . $mysql_keys . ")") || print "died again sth1 consortium\n";

unlink($next_file);

}

}



if($ARGV[0] ne 'CREATING') {

$this_table = $db_consortium_tables_hash{'items_record_table'};
$that_table = $db_consortium_tables_hash{'bibs_record_table'};

$update_string = "UPDATE $that_table, $this_table
set $that_table.solr_update = NOW()
WHERE $this_table.solr_update >= ?
AND $that_table.local_control_number = $this_table.local_control_number";

$query = $dbh->prepare($update_string)
                    or die("couldn't prepare statement: ". $dbh->errstr);

                $query->execute($last_update_time)
                    or die("couldn't execute statement: ". $dbh->errstr);

                $query->finish();

$limit_start = 0;
$rownumber = 1;
while($rownumber > 0) {

        $querystring = "SELECT COUNT( * ) AS n, universal_identifier
            FROM $that_table
            WHERE $that_table.solr_update >= ?
            GROUP by universal_identifier
            HAVING n > 1
            LIMIT ?, 10000";

        $query = $dbh->prepare($querystring)
                    or die("couldn't prepare statement: ". $dbh->errstr);

                $query->execute($last_update_time, $limit_start)
                    or die("couldn't execute statement: ". $dbh->errstr);

    $limit_start += 10000;
    $rownumber = $query->rows();

    while ( @row = $query->fetchrow_array ) {

$update_string = "UPDATE $that_table
    set $that_table.solr_update = NOW()
    WHERE $that_table.universal_identifier = $row[0]";

$update_query = $dbh->prepare($update_string)
                    or die("couldn't prepare statement: ". $dbh->errstr);

                $update_query->execute()
                    or die("couldn't execute statement: ". $dbh->errstr);

                $update_query->finish();
        }

}
                $query->finish();

}

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$end_external_update_time = "$HOUR:$MIN:$SEC";


$dbh->disconnect;

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$end_time = "$HOUR:$MIN:$SEC";

print "Start time: " . $start_time . "\nEnd of file creation time: " . $end_of_file_creation_time . "\nEnd time: " . $end_time . "\n Number of files: " . $total_number_of_files . "\n";
print "start_external_update_time: " . $start_external_update_time . "\n";
print "end_external_update_time: " . $end_external_update_time . "\n";

close (STDERR) || "cannot close ONEW_errors.txt: $!";
close (STDOUT) || "cannot close ONEW_prints.txt: $!";


#SUBROUTINES

sub hashes_and_arrays {

$hash_files = $baseurl . '/hashfiles';

while (defined($next_hash_file = <$hash_files/*.pl>)) {
require $next_hash_file;
}

}
